﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSTDZ.ERP.Application.DataContracts;
using AutoMapper;
using XSTDZ.ERP.Domain.Models;
using XSTDZ.ERP.Core.Repositories;
using XSTDZ.ERP.Core.Generator;

namespace XSTDZ.ERP.Application.Implements
{
    public class RefeServiceImpl : ApplicationServiceBase, IRefeService
    {
        IRepository<RefeType> _refeTypeRepository;
        IRepository<Refe> _refeRepository;
        IRepository<DataBack> _backUpRepository;
        public RefeServiceImpl(IRepositoryContext repositoryContext, IRepository<RefeType> refeTypeRepository, IRepository<Refe> refeRepository, IRepository<DataBack> backUpRepository)
            : base(repositoryContext)
        {
            this._refeTypeRepository = refeTypeRepository;
            this._refeRepository = refeRepository;
            this._backUpRepository = backUpRepository;
        }

        public void AddRefe(Guid refeTypeId, RefeDto refeDto)
        {
           
            var refe = Mapper.Map<RefeDto, Refe>(refeDto);
            refe.RefeTypeId = refeTypeId;
            refe.Id = (Guid)new SequentialIdentityGenerator().Next;
            refe.Init(refe);
            _refeRepository.Add(refe);
            this.Context.Commit();
        }

        public string GetNextBuessionDh(string buesssionTypeName,string currentDh,bool isUpdate)
        {
            var refe = this._refeTypeRepository.Get(t => t.ModuleName.Trim() == "业务单据号");
            if (refe != null)
            {
                if (refe.Refes != null)
                {
                    foreach (var item in refe.Refes)
                    {
                        if (item.RefeCode.Trim() == buesssionTypeName)
                        {
                            if (isUpdate && currentDh.Trim().Length > 1)
                            {
                                item.RefeName = currentDh;
                                this._refeTypeRepository.Update(refe);
                                this.Context.Commit();
                            }
                            return item.RefeName;
                            break;
                        }
                    }
                }
            }
            return "";
        }

        public void UpdateRefeTypeState(Guid refeTypeId)
        {
          var refeType=  _refeTypeRepository.GetByKey(refeTypeId);
          refeType.SetDeleteState(refeType);
          _refeTypeRepository.Update(refeType);
          this.Context.Commit();
        }

        public void UpdateRefeState(Guid refeId)
        {
            var refe = _refeRepository.Get(r => r.Id == refeId);
            refe.Deleted = 1;
            _refeRepository.Update(refe);
            this.Context.Commit();
        }

        public RefeTypeDto GetRefeTypeDto(Guid refeTypeId)
        {
            var refeType = _refeTypeRepository.Get(r => r.Id == refeTypeId);
            return Mapper.Map<RefeType, RefeTypeDto>(refeType);
        }

        public RefeDto GetRefeDto(Guid refeId)
        {
            var refe = _refeRepository.Get(r => r.Id == refeId);
            return Mapper.Map<Refe, RefeDto>(refe);
        }

        public IEnumerable<RefeTypeDto> GetAllRefeTypeDtos()
        {
            var refeType = _refeTypeRepository.GetAll();
            return Mapper.Map<IEnumerable<RefeType>, IEnumerable<RefeTypeDto>>(refeType);
        }


        public IEnumerable<RefeDto> GetRefeDtos(Guid refeTypeId)
        {
            var refes = _refeRepository.GetAll(r => r.RefeTypeId == refeTypeId);
            return Mapper.Map<IEnumerable<Refe>, IEnumerable<RefeDto>>(refes);
        }


        public IEnumerable<RefeDto> GetRefeDtosByModuleName(string moduleName)
        {
            var refes = _refeTypeRepository.Get(t => t.ModuleName == moduleName);
            return Mapper.Map<IEnumerable<Refe>, IEnumerable<RefeDto>>(refes.Refes);
        }

        #region 数据库数据备份
        
       
        public bool DataBaseBackUp(DataBackDto dataBackDto)
        {
            //var dataBackUp = Mapper.Map<DataBackDto, DataBack>(dataBackDto);
            var bill = this._backUpRepository.GetAll().First();
            bill.BackTime = dataBackDto.BackTime;
            bill.BackCount = dataBackDto.BackCount;
            this._backUpRepository.Update(bill);
            this.Context.Commit();
            return true;
        }

        public DataBackDto GetDataBackDto()
        {
            var dataBackUp = _backUpRepository.GetAll().FirstOrDefault();
            return Mapper.Map<DataBack, DataBackDto>(dataBackUp);
        }

        #endregion
    }
}
